let mta = require('../../utils/mta_analysis.js');
let fetch = require("../../utils/fetch");
Page({
	data: {
		urlAddress: getApp().globalData.imageUrl,
		msg1: '',
		msg2: '',
		passWord1: '',
		passWord1: '',
		type: '',
		isRight: false
	},
	onLoad: function(options) {
		if (options.souce == "1" || options.souce == "2") { //1新用户 2,忘记密码
			wx.setNavigationBarTitle({
				title: '余额密码设置'
			})
			this.setData({
				msg1: "设置密码",
				msg2: "重复密码"
			})
		} else if (options.souce == "0") { //重置密码
			wx.setNavigationBarTitle({
				title: '修改密码'
			})
			this.setData({
				msg1: "原密码",
				msg2: "新密码"
			})
		}
		this.setData({
			type: options.souce
		})
		console.log("type==" + this.data.type)
	},
	onReady: function() {},
	onShow: function() {

	},
	onHide: function() {},
	onUnload: function() {},
	getPassWord1: function(e) {
		console.log(e.detail.value)
		this.setData({
			passWord1: e.detail.value
		})
		if (this.data.type == '0' & e.detail.value.length == 6) {
			this.filterPassword1(e.detail.value)
		}
	},
	getPassWord2: function(e) {
		console.log(e.detail.value)
		this.setData({
			passWord2: e.detail.value
		})
	},
	nextStop: function() {

		var _self = this;
		if (this.data.type == 1 || this.data.type == 2) { //第一次设置密码或者忘记密码
			if (this.data.passWord1 == this.data.passWord2 && this.data.passWord1.length == 6) {
				wx.request({
					url: getApp().globalData.baseUrl + '/applet/balance/pwd/set/' + _self.data.type + '.do?password=' + _self.data.passWord1,
					method: 'post',
					header: getApp().globalData.header,
					success: function(res) {
						if (res.data.code == 200) {

							if (getApp().globalData.common.isBalance) {
								if (_self.data.type == 1) {
									_self.filterPassword(_self.data.passWord1)
								} else if (_self.data.type == 2) {
									wx.switchTab({
										url: '../mine/mine'
									})
								} else {

									_self.filterPassword1(_self.data.passWord1)
								}

							} else {
								wx.switchTab({
									url: '../mine/mine'
								})
							}


						}
					}
				})

			} else {
				fetch.showMyToast(_self, "密码必须一致的6位数字")
			}

		} else if (this.data.type == 0 && this.data.isRight) { //修改密码
			if (this.data.passWord1.length < 6) {
				fetch.showMyToast(this, "原密码错误");
				return
			}
			if (this.data.passWord2.length < 6) {
				fetch.showMyToast(this, "密码必须是6位数字");
				return
			}
			wx.request({
				url: getApp().globalData.baseUrl + '/applet/balance/pwd/set/3.do?password=' + _self.data.passWord2,
				method: 'post',
				header: getApp().globalData.header,
				success: function(res) {
					if (res.data.code == 200) {
						wx.switchTab({
							url: '../mine/mine'
						})
					}
				}
			})

		} else {
			fetch.showMyToast(this, "原密码错误");
		}

	},
	filterPassword1: function(num) {
		var _self = this;
		wx.showLoading({
			title: '加载中',
			mask: true
		})
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/balance/pwd/check.do?password=' + num,
			method: 'post',
			header: getApp().globalData.header,
			success: function(res) {
				wx.hideLoading()
				console.log(123);
				if (res.data.code == 200) {
					_self.setData({
						isRight: true
					})
					fetch.showMyToast(_self, "原密码正确")
				} else {
					_self.setData({
						isRight: false
					})
					fetch.showMyToast(_self, "原密码错误")
				}
			}

		})

	},
	forget: function() {
		wx.navigateTo({
			url: '../balanceLoding/balanceLoding'
		})
	},
	filterPassword: function(num) { //校验验证码
		var _self = this;
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/balance/pwd/check.do?password=' + num,
			method: 'post',
			header: getApp().globalData.header,
			success: function(res) {
				if (res.data.code == 200) {

					_self.openBalance();


				} else {
					_self.setData({
						balanceStatus: false,
						isShow: false,
						styleList: ['noSelect_style', 'noSelect_style', 'noSelect_style', 'noSelect_style', 'noSelect_style', 'noSelect_style']
					})
					fetch.showMyToast(_self, "密码错误")
				}

			}

		})

	},
	openBalance: function() {
		var _self = this;
		wx.showLoading({
			title: '加载中',
			mask: true
		})
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/balance/pwd/sign.do',
			method: 'get',
			header: getApp().globalData.header,
			success: function(res) {
				console.log(res)
				wx.hideLoading();
				if (res.data.code == 200) {
					wx.redirectTo({
						url: '../freePayment/freePayment'
					})

				} else {

				}

			}
		})



	}


})